package com.yx.service.init;

import com.yx.entity.SysMenu;
import com.yx.entity.SysPermission;
import com.yx.mapper.SysMenuMapper;
import com.yx.mapper.SysPermissionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Description: <br />
 * 初始化系统菜单
 * <p>
 * <br />
 * @Auther: Zhangjiashun
 * @create 2019/11/22 16:22
 */
@Component
@Slf4j
public class InitSysMenu {
    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    SysPermissionMapper sysPermissionMapper;

    public static final String SYS_MENU = "sys:menu:manager";


    public void init(){
        SysPermission permission = sysPermissionMapper.getInfo(SYS_MENU);
        if (Objects.isNull(permission)) {
            SysPermission sysPermission = new SysPermission();
            sysPermission.setPermId(SYS_MENU);
            sysPermission.setPermName("系统菜单:管理");
            this.sysPermissionMapper.insert(sysPermission);
        }
        int count = sysMenuMapper.getCount(new SysMenu());
        if (0 == count) {
            log.info("数据库中无菜单信息，初始化 菜单。。。");
            addMenu("el-icon-menu", "系统管理", "0", "systemMg", 99, SYS_MENU,"1");
            addMenu("el-icon-user", "用户管理", "1", "userMg", 1, "sys:user:list",null);
            addMenu("el-icon-female", "角色管理", "1", "roleMg", 2, "sys:role:list",null);
            addMenu("el-icon-guide", "权限管理", "1", "authorityMg", 3, "sys:permission:list",null);
            addMenu("el-icon-tickets", "菜单管理", "1", "menuMg", 4, "sys:menu:list",null);
        }

    }

    private void addMenu(String icon,String menuName,String menuParent,String menuUri,Integer sort,String permissionId,String menuId){
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuIcon(icon);
        sysMenu.setMenuName(menuName);
        sysMenu.setMenuParent(menuParent);
        sysMenu.setMenuUri(menuUri);
        sysMenu.setMenuSort(sort);
        sysMenu.setPermissionId(permissionId);
        sysMenu.setMenuId(menuId);
        sysMenuMapper.insert(sysMenu);
    }
}
